package com.ruoyi.common.security.utils;


import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.redis.service.RedisService;


import java.util.*;

/**
 * @author yyh
 * @date 2024/3/5 15:21
 * @description PigUtils
 * 猪只缓存工具类
 */
public class PigUtils {

    /**
     * 设置猪只缓存
     * @param code 猪代号
     * @param pig 实体类
     */
    public static void setPigCache(String code, Object pig){
        SpringUtils.getBean(RedisService.class).setCacheObject(getCacheKey(code),pig);
    }

    /**
     * 设置设备数据缓存
     * @param map
     * @param <T>
     */
    public static <T> void setEquipmentCache(HashMap<String,List<T>> map){
        for (String key : map.keySet()) {
            List<T> list = map.get(key);
            for (int i = 0; i < list.size(); i++) {
                SpringUtils.getBean(RedisService.class).setCacheObject(getEquipmentCacheKey(key)+i,list.get(i));
            }
        }
    }

    /**
     * 根据houseCode获得对应的值
     * @param code
     */
    public static List<Object> getEquipmentCacheByCode(String code){
        return SpringUtils.getBean(RedisService.class).getAllDataFromDirectory(getEquipmentCacheKey(code));
    }



    /**
     * 获取所有猪只信息
     * @return
     */
    public static List<Object> getPigCacheAll() {
        List<Object> list = SpringUtils.getBean(RedisService.class).getAllDataFromDirectory(CacheConstants.HMY_PIG_KEY);
        return list;
    }

    public static List<Object> getEquipmentAll(){
        List<Object> list = SpringUtils.getBean(RedisService.class).getAllDataFromDirectory(CacheConstants.HMY_EQUIPMENT_KEY);
        return list;
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private static String getCacheKey(String configKey)
    {
        return CacheConstants.HMY_PIG_KEY + configKey;
    }

    /**
     * 设置二级文件夹
     * @param key 集合键名
     * @return
     */
    private static String getEquipmentCacheKey(String key)
    {
        return CacheConstants.HMY_EQUIPMENT_KEY + key + ":";
    }



}
